package com.mdd.admin.controller;

import com.mdd.admin.LikeAdminThreadLocal;
import com.mdd.admin.aop.Log;
import com.mdd.admin.service.IInventorService;
import com.mdd.admin.validate.commons.IdValidate;
import com.mdd.admin.validate.InventorCreateValidate;
import com.mdd.admin.validate.InventorUpdateValidate;
import com.mdd.admin.validate.InventorSearchValidate;
import com.mdd.admin.validate.commons.PageValidate;
import com.mdd.admin.vo.InventorListedVo;
import com.mdd.admin.vo.InventorDetailVo;
import com.mdd.common.core.AjaxResult;
import com.mdd.common.core.PageResult;
import com.mdd.common.validator.annotation.IDMust;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

@RestController
@RequestMapping("api/inventor")
@Api(tags = "发明人列管理")
public class InventorController {

    @Resource
    IInventorService iInventorService;

    @GetMapping("/list")
    @ApiOperation(value="发明人列列表")
    public AjaxResult<PageResult<InventorListedVo>> list(@Validated PageValidate pageValidate,
                                                     @Validated InventorSearchValidate searchValidate) {
        PageResult<InventorListedVo> list = iInventorService.list(pageValidate, searchValidate);
        return AjaxResult.success(list);
    }

    @GetMapping("/all")
    @ApiOperation(value="发明人列列表")
    public AjaxResult<List<InventorListedVo>> all(@Validated InventorSearchValidate searchValidate) {
        searchValidate.setUserId(LikeAdminThreadLocal.getAdminId());
        List<InventorListedVo> list = iInventorService.all(searchValidate);
        return AjaxResult.success(list);
    }

    @GetMapping("/detail")
    @ApiOperation(value="发明人列详情")
    public AjaxResult<InventorDetailVo> detail(@Validated @IDMust() @RequestParam("id") Integer id) {
        InventorDetailVo detail = iInventorService.detail(id);
        return AjaxResult.success(detail);
    }

    @Log(title = "发明人列新增")
    @PostMapping("/add")
    @ApiOperation(value="发明人列新增")
    public AjaxResult<Object> add(@Validated @RequestBody InventorCreateValidate createValidate) {
        iInventorService.add(createValidate);
        return AjaxResult.success();
    }

    @Log(title = "发明人列编辑")
    @PostMapping("/edit")
    @ApiOperation(value="发明人列编辑")
    public AjaxResult<Object> edit(@Validated @RequestBody InventorUpdateValidate updateValidate) {
        iInventorService.edit(updateValidate);
        return AjaxResult.success();
    }

    @Log(title = "发明人列删除")
    @PostMapping("/del")
    @ApiOperation(value="发明人列删除")
    public AjaxResult<Object> del(@Validated @RequestBody IdValidate idValidate) {
        iInventorService.del(idValidate.getId());
        return AjaxResult.success();
    }

}
